package com.css.fxfzmh.zcpt.sys.controller;

import com.css.fxfzmh.zcpt.sys.entity.ReturnAreaTree;
import com.css.fxfzmh.zcpt.sys.service.SysOrgService;
import com.css.fxfzmh.base.response.RestResponse;
import com.css.fxfzmh.util.CreateGson;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Dbj
 * @date 2022年08月10日  11:17
 */
@RestController
@RequestMapping("/fxfz/sOrg")
@Slf4j
public class SysOrgController {
    @Autowired
    private SysOrgService sysOrgService;
    /**
     * 获取全部组织机构树
     */
    @GetMapping("/jsonSOrgToDeptTree")
    public RestResponse jsonSOrgToDeptTree(@RequestParam("orgId") String orgId) {
        RestResponse restResponse = null;
        try{
            String jsonStr = sysOrgService.jsonSOrgToDeptTree(orgId);
            Gson gson = CreateGson.createGson();
            List sysMenuList = gson.fromJson(jsonStr, List.class);
            restResponse = RestResponse.succeed(sysMenuList);
        }catch (Exception e){
            log.error("查询机构失败!",e);
            restResponse = RestResponse.fail("查询机构失败!");
        }
        return restResponse;
    }

    /**
     * 根据父节点ID获取组织机构树
     */
//    @GetMapping("/listOrgTree1")
//    public RestResponse listOrgTree1(@RequestParam("orgId") String orgId) {
//        RestResponse restResponse = null;
//        try{
//            List<ReturnAreaTree> trees = sysOrgService.listOrgTree(orgId);
//            restResponse = RestResponse.succeed(trees);
//        }catch (Exception e){
//            log.error("查询机构失败!!",e);
//            restResponse = RestResponse.fail("查询机构失败!!");
//        }
//        return restResponse;
//    }


    /**
     * 根据父节点ID获取组织机构树
     */
    @GetMapping("/listOrgTree")
    public RestResponse listOrgTree(@RequestParam("orgId") String orgId) {
        RestResponse restResponse = null;
        try{
            long start = System.currentTimeMillis();
            ReturnAreaTree tree = sysOrgService.listOrgTree2(orgId);
            restResponse = RestResponse.succeed(tree);
            long end = System.currentTimeMillis();
            long l = TimeUnit.MILLISECONDS.toSeconds(end - start);
            System.out.println(l);
        }catch (Exception e){
            log.error("查询机构失败!!",e);
            restResponse = RestResponse.fail("查询机构失败!!");
        }
        return restResponse;
    }

    /**
     * 根据父节点ID获取组织机构树
     * parentId=0 显示国家级
     *        =国家级id 显示省级
     *        = 省级id  显示市级
     *        = 市级id 显示区县级
     */
    @GetMapping("/getOrgData")
    public RestResponse getOrgData(@RequestParam("parentId") String parentId) {
        RestResponse restResponse = null;
        try{
            String jsonStr = sysOrgService.getOrgDataByParentId(parentId);
            Gson gson = CreateGson.createGson();
            List sysMenuList = gson.fromJson(jsonStr, List.class);
            restResponse = RestResponse.succeed(sysMenuList);
        }catch (Exception e){
            log.error("查询机构失败!",e);
            restResponse = RestResponse.fail("查询机构失败!");
        }
        return restResponse;
    }
}
